//
//  LBMVisitorLoginView.swift
//  微博
//
//  Created by 刘保满 on 16/5/20.
//  Copyright © 2016年 itheima. All rights reserved.
//




import UIKit
//引用第三方框架需要导入命名空间
import SnapKit


//MARK:想把按钮的点击事件进行传递,让控制器去出来,使用代理处理
@objc protocol LBMVisitorLoginViewDelegate: NSObjectProtocol  {
    
    //声明协议方法
    optional func userWillLogin()
    
    optional func userWillRegister()
    

}






class LBMVisitorLoginView: UIView {
    
    
    //声明代理属性
   weak var visitorDelegate: LBMVisitorLoginViewDelegate?
    
    
    
    
    //MARK:定义一个方法供外界访问
    func updateUI(title: String, imageName: String, isHome: Bool = false) {
        if isHome {
            
            startAnimation()
            
        } else {
            //不是home页,让大房子隐藏
            largeHouse.hidden = true
            //把大圈圈移到最顶层
            bringSubviewToFront(circleIon)
            
        }
        circleIon.image = UIImage(named: imageName)
        tipLabel.text = title

    }
    
 
    //MARK:访客视图
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
        
    }
    
    //只要重写了上面的方法就要实现这个方法,告诉程序员只能用纯代码实现功能,不可以用xib,否则会默认报错
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
    //给大圆圈设置动画效果
    private func startAnimation() {
        
        let basic = CABasicAnimation(keyPath: "transform.rotation")
        
        //动画时间
        basic.duration = 15
        
        //动画重复次数
        basic.repeatCount = MAXFLOAT
        
        basic.toValue = 2 * M_PI
        
        //当从首页跳到下个界面,再回来动画会消失,加上下面这句动画效果不移除图层
        basic.removedOnCompletion = false
        
        circleIon.layer.addAnimation(basic, forKey: nil)
        
    }
    
    
  
    
    //定义一个函数用来设置UI界面,添加子控件
    private func setupUI() {
        
        //添加子控件
        addSubview(circleIon)
        //需要把遮罩添加在大房子和大圆圈之间避免遮罩大房子
        addSubview(backIcon)
        
        addSubview(largeHouse)
        addSubview(tipLabel)
        addSubview(loginBtn)
        addSubview(registernBtn)

        //设置最大的布局宽度
        tipLabel.preferredMaxLayoutWidth = 230
        
        
        //MARK:利用第三方框架设置约束
        //中间的大圆圈
        circleIon.snp_makeConstraints { (make) in
            
            //参数make 是已经取消了frame布局的对象
            make.centerX.equalTo(self.snp_centerX)
            
            make.centerY.equalTo(self.snp_centerY).offset(-80)
            
        }
        
        //中间的大房子
        largeHouse.snp_makeConstraints { (make) in
            
            //            make.centerX.equalTo(circleIon.snp_centerX)
            //            make.centerY.equalTo(circleIon.snp_centerY)
            //上面两句可以简写
            make.center.equalTo(circleIon.snp_center)
            
            
        }
        
        //房子下面的文字
        tipLabel.snp_makeConstraints { (make) in
            
            make.centerX.equalTo(circleIon.snp_centerX)
            make.top.equalTo(circleIon.snp_bottom).offset(16.0)
            
            //同样的原理
            make.width.equalTo(230)
            
            //因为登录和注册按钮的顶部是根据文字的底部进行约束的,如果不约束文字的高度,由于每个界面的文字行数不一致,会造成页面跳动,所以要设置高度约束
            make.height.equalTo(40)
            
            
        }
        
        //登录按钮
        loginBtn.snp_makeConstraints { (make) in
            
            make.left.equalTo(tipLabel.snp_left)
            make.top.equalTo(tipLabel.snp_bottom).offset(16.0)
            
            make.width.equalTo(100)
            
        }
        
        //注册按钮
        registernBtn.snp_makeConstraints { (make) in
            
            make.right.equalTo(tipLabel.snp_right)
            
            make.top.equalTo(tipLabel.snp_bottom).offset(16.0)
            
            make.width.equalTo(100)
        
        }
        
        //遮罩
        backIcon.snp_makeConstraints { (make) in
            
            make.center.equalTo(circleIon.snp_center)
            
        }
        
        //为了让背景和遮罩的颜色一样所以需要设置背景颜色,用sRGB测出来237
        backgroundColor = UIColor(red: 237/255.0, green: 237/255.0, blue: 237/255.0, alpha: 1.0)

        
        
        //添加注册登录按钮的点击事件
        loginBtn.addTarget(self, action: #selector(LBMVisitorLoginView.loginBtnDidClick), forControlEvents: .TouchUpInside)
        
        registernBtn.addTarget(self, action: #selector(LBMVisitorLoginView.registernBtnDidClick), forControlEvents: .TouchUpInside)
        

        
        
        
    }
    
    
    //MARK:按钮的点击事件
    //登录
    @objc private func loginBtnDidClick() {
        
        //第一个"?" 表示代理属性是可选的,代理对象是否为空
        //第二个"?" 表示协议方法是可选的,是否响应协议方法
        visitorDelegate?.userWillLogin?()
        
        
    }
    //注册按钮
    @objc private func registernBtnDidClick() {
        
        visitorDelegate?.userWillRegister?()
        
    }
    
    
    
    
    
    
    
    //MARK:懒加载子控件
    //用来遮挡圆圈的下半部分
    private lazy var backIcon: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    
    
    //中间的大圆圈
//    private lazy var circleIon: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    private lazy var circleIon: UIImageView = UIImageView()
    
    
    //中间的房子
    private lazy var largeHouse: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    //中间的文字
    private lazy var tipLabel: UILabel = UILabel(title: "关注一些人,回到这里看看有什么惊喜", font: 14.0, textColor: UIColor.darkGrayColor())
        
    
    
    //登录按钮
    private lazy var loginBtn: UIButton = {
        let logbtn = UIButton()
        
        logbtn.setTitle("登录", forState: .Normal)
        logbtn.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        
        logbtn.titleLabel?.font = UIFont.systemFontOfSize(15.0)
        
        //图片需要切图
        let image = UIImage(named: "common_button_white_disable")!
        let capW = Int(image.size.width * 0.5)
        let capH = Int(image.size.height * 0.5)
        
         logbtn.setBackgroundImage(image.stretchableImageWithLeftCapWidth(capW, topCapHeight: capH), forState: .Normal)
   
        
        return logbtn
        
        
    }()
    
    //注册按钮
    private lazy var registernBtn: UIButton = {
        let btn = UIButton()
        
        btn.setTitle("注册", forState: .Normal)
        btn.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        
        btn.titleLabel?.font = UIFont.systemFontOfSize(15.0)
        
        let image = UIImage(named: "common_button_white_disable")!
        let capW = Int(image.size.width * 0.5)
        let capH = Int(image.size.height * 0.5)
        
        btn.setBackgroundImage(image.stretchableImageWithLeftCapWidth(capW, topCapHeight: capH), forState: .Normal)
        
        return btn
        
        
    }()
    
  
    
}
